<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Jingshu as JingshuModel;
use app\common\model\JingshuProgress as JingshuProgressModel;
use app\common\model\JingshuWord as JingshuWordModel;

/**
 * 经书进度控制器
 * Class Jingshuprogress
 * @package app\home\controller
 */
class Jingshuprogress extends Base
{
    /**
     * [index  获取抄经记录]
     * @param
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new JingshuProgressModel;
        $jmodel = new JingshuModel;
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try{
            $map['status'] = 1;
            $map['mid'] = $user['id'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map,$this->from, $this->size,'id,jid,update_time');
            foreach($lists as &$v){
                $jingshu = $jmodel->findData($v['jid'],'id','title');
                $v['title'] = $jingshu['title'];
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [get  根据id获取]
     * @param
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $pmodel = new JingshuProgressModel();
        $wmodel = new JingshuWordModel;
        $user = $this->user;
        try{
            if(empty($param['pid'])){
                $progress = $pmodel->findOne(['mid'=>$user['id'],'jid'=>$param['jid'],'status'=>2]);
                if(!empty($progress)){
                    return admin_json(-1, '请完成了再来');
                }
                $param['mid'] = $user['id'];
                $id = $pmodel->addData($param);
                $wmodel->addData(['img'=>$param['img'],'pid'=>$id,'word'=>$param['word']]);
            }else{
                $progress = $pmodel->findData($param['pid'],'id','mid');
                if($progress['mid'] != $user['id']){
                    return admin_json(-1, '你走错门了');
                }
                $id = $pmodel->editData(['id'=>$param['pid'],'number'=>$param['number']]);
                $wmodel->addData(['sort'=>$param['number'],'img'=>$param['img'],'pid'=>$param['pid'],'word'=>$param['word']]);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '失败'.$e->getMessage());
        }
        return admin_json(1, '成功',$id);
    }
    /**
     * [get  根据id获取]
     * @param
     * @return json
     * @author [C。]
     */
    public function addhuixiang()
    {
        $param = input('param.');
        $model = new JingshuProgressModel();
        $user = $this->user;
        try{
            $model->editData(['id'=>$param['pid'],'huixiang'=>$param['huixiang'],'status'=>1]);
            add_quality($user['id'],8,$user['quality'],2);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '失败'.$e->getMessage());
        }
        return admin_json(1, '成功');
    }
    /**
     * [gethuixiang  获取回向]
     * @param
     * @return json
     * @author [C。]
     */
    public function gethuixiang()
    {
        $param = input('param.');
        $model = new JingshuProgressModel();
        try{
            $data = $model->findData($param['pid'],'id','huixiang');
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '失败'.$e->getMessage());
        }
        return admin_json(1, '成功',$data);
    }
}